﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PostMsg_Net;
using System.Net;
using CRM.BLL;
using DP.Web.UI;
using CRM.Entity;

namespace CRM.Web.MessagingManage
{
    public partial class SMSBalance : Common.PageBase
    {
        private T_BUSINESSDIVISION_BLL businessDivisionBll = new T_BUSINESSDIVISION_BLL();

        private PostMsg msg = new PostMsg("", "");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBusinessDivisionBinding();
            }
            
        }

        private void ddlBusinessDivisionBinding()
        {
            List<T_BUSINESSDIVISION_Entity> list = businessDivisionBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBusinessDivision.DataSource = list;
            ddlBusinessDivision.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBusinessDivision.DataValueField = "BUSINESSDIVISIONID";
            ddlBusinessDivision.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_BUSINESSDIVISION_Entity businessDivisionEntity = businessDivisionBll.GetSession_T_BUSINESSDIVISION_List(this.Page).Find(p=> p.BUSINESSDIVISIONID.ToString() == ddlBusinessDivision.SelectedValue.Trim());

            string account = businessDivisionEntity.SMS_UID;
            string password = businessDivisionEntity.SMS_PWD;
            
            string gatewayServerAddress = configBll.GetCacheValueByName(this.Page, "GatewayServerAddress");
            string gatewayServerPort = configBll.GetCacheValueByName(this.Page, "GatewayServerPort");
            if (String.IsNullOrEmpty(gatewayServerAddress))
            {
                gatewayServerAddress = "211.147.224.154";
            }
            if (String.IsNullOrEmpty(gatewayServerPort))
            {
                gatewayServerPort = "13013";
            }

            msg.GatewayServer.Address = IPAddress.Parse(gatewayServerAddress);
            msg.GatewayServer.Port = int.Parse(gatewayServerPort);

            msg.SetUser(account, password);

            try
            {
                lblBalance.Text = msg.GetRemainFee().ToString();
            }
            catch (Exception ex)
            {
                lblBalance.Text = ex.Message;
            }


        }
    }
}
